/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oohadv.dao.impl;

import com.oohadv.dao.ibase.GenericDAO;
import com.oohadv.util.HibernateUtil;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author vinits
 * @param <T>
 * @param <ID>
 */
public abstract class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> {

    private Class<T> persistentClass;

    public GenericDAOImpl() {
        System.out.println("getClass()" +getClass());
        System.out.println("getGenericSuperclass()" +getClass().getGenericSuperclass());
        persistentClass = (Class<T>) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
    }

    protected Session getSession() {
        return HibernateUtil.getSessionFactory().openSession();
    }

    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    @Override
    public List<T> findAll() {
        Session session = getSession();
        List<T> list=null;
        try{
            list=session.createCriteria(persistentClass).list();
        }catch(Exception e){
            System.out.println("Exception while fetching" + e);
        }finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return list;
    }

    @Override
    public List<T> findByRange(int startLimit, int maxLimit) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ID save(T entity) {
        Session session = getSession();
        Transaction tx = null;
        ID id = null;
        try {
            tx = session.beginTransaction();
            id = (ID) session.save(entity);
            tx.commit();
        } catch (Exception e) {
            System.out.println("Exception : " + e);
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return id;
    }

    @Override
    public boolean update(T entity) {
        Session session = getSession();
        Transaction tx = null;
        boolean isUpdated = false;
        try {
            tx = session.beginTransaction();
            session.update(entity);
            tx.commit();
            isUpdated = true;
        } catch (Exception e) {
            System.out.println("Exception : " + e);
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return isUpdated;
    }

    @Override
    public boolean delete(T entity) {
        Session session = getSession();
        Transaction tx = null;
        boolean isDeleted = false;
        try {
            tx = session.beginTransaction();
            session.delete(entity);
            tx.commit();
            isDeleted = true;
        } catch (Exception e) {
            System.out.println("Exception : " + e);
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return isDeleted;
    }

    @Override
    public T findById(ID id) {
        Session session = getSession();
        T domainObj=null;
        try{
            domainObj=(T)session.get(persistentClass, id);
        }catch(Exception e){
            System.out.println("Exception while fetching by id" + e);
        }finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return domainObj;
    }

}
